package com.red.wood.common.util;

import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
 
/**
 * 包装类
 */
public class ExportExcelWrapper extends ExportExcelUtil {
	
	private static final Log log = LogFactory.getLog(ExportExcelWrapper.class);
	
	/**
	 * @param title 表格标题
	 * @param headers 头部标题集合
	 * @param dataset 数据集合
	 * @param out 输出流
	 * @param version 2003 或者 2007，不传时默认生成2003版本
	 */
	public void exportExcel(String title,String fileName, String[] headers,String[] columnsWidth,String[] mapKey, List<Map<String,Object>> dataset, HttpServletResponse response,String version) {
		try {
			response.setContentType("application/vnd.ms-excel");  
    		//response.addHeader("Content-Disposition", "attachment;filename="+ URLEncoder.encode(fileName, "UTF-8") + ".xlsx");
			response.setHeader("content-disposition","attachment; filename=" + URLEncoder.encode(fileName, "UTF-8") + ".xlsx");

    		if(StringUtils.isBlank(version) || EXCEL_FILE_2003.equals(version.trim())){
				exportExcel2003(title, headers,columnsWidth,mapKey, dataset, response.getOutputStream());
			}else{
				exportExcel2007(title, headers,columnsWidth,mapKey,dataset, response.getOutputStream());
			}
		} catch (Exception e) {
			if(log.isErrorEnabled()) {
				log.error(e.getMessage(),e);
			}
		}
	}
}
